//
//  SGCorePayment.m
//  smartGas
//
//  Created by chen on 2017/11/20.
//  Copyright © 2017年 chen. All rights reserved.
//

#import "SGCorePayment.h"
#import "WXApi.h"
#import "HTTP.h"

@implementation SGGeneratedWeixinPayModel
@end

@implementation SGCorePayment

#pragma mark - 微信支付
/*
 如何集成微信支付集成:
 1. pods
 2. URL type 添加从微信注册的应用程序id ,如 wx4318564cfc17c463 
 3. `AppDelegate+SGPaymentOpenURL`
 4. 下单操作
 */
+ (void)wxPayTest{
    //这些数据需要后台传过来
    PayReq *request = [[PayReq alloc] init] ;
    // 商家向财付通申请的商家id(后台传过来)
    request.partnerId = @"10000100";
    /** 商家根据财付通文档填写的数据和签名: 固定的*/
    // 预支付订单
    // 预支付订单这个是后台跟微信服务器交互后，微信服务器传给服务器的，服务器再传给app
    request.prepayId= @"1101000000140415649af9fc314aa427";
    request.package = @"Sign=WXPay";
    /** 随机串，防重发 (后台生成)*/
    request.nonceStr= @"a462b76e7436e98e0ed6e13c64b4fd1c";
    /** 时间戳，防重发 (后台生成)*/
    request.timeStamp= 1397527777;
    /** 商家根据微信开放平台文档对数据做的签名 (后台生成)*/
    request.sign= @"582282D72DD2B03AD892830965F428CB16E7A256";
    [WXApi sendReq:request];
}


/**
 发起微信支付

 @param partnerId 商家id
 @param prepayId 预支付订单
 @param nonceStr 随机串，防重发
 @param timeStamp 时间戳，防重发
 @param sign 数据签名
 */
+ (void)wxPayWithPartnerId:(NSString *)partnerId
                  prepayId:(NSString *)prepayId
                  nonceStr:(NSString *)nonceStr
                 timeStamp:(UInt32)timeStamp
                      sign:(NSString *)sign{
    
    PayReq *request = [[PayReq alloc] init] ;
    // 商家向财付通申请的商家id(后台传过来)
    request.partnerId = partnerId;
    // 预支付订单
    // 预支付订单这个是后台跟微信服务器交互后，微信服务器传给服务器的，服务器再传给app
    request.prepayId= prepayId;
    /** 商家根据财付通文档填写的数据和签名: 固定的*/
    request.package = @"Sign=WXPay";
    /** 随机串，防重发 (后台生成)*/
    request.nonceStr= nonceStr;
    /** 时间戳，防重发 (后台生成)*/
    request.timeStamp= timeStamp;
    /** 商家根据微信开放平台文档对数据做的签名 (后台生成)*/
    request.sign= sign;
    [WXApi sendReq:request];
}




//微信生成订单
+ (void)wxGeneratePaymentWithUserId:(NSString *)userId
                             amount:(NSString *)amount
                               type:(SGCorePaymentType)paymentType
                          goodsType:(SGCorePaymentGoodsType)goosType
                         relationId:(NSString *)relationId
                            success:(void(^)(SGGeneratedWeixinPayModel *model,NSString *des))success
                             failed:(void(^)(NSError *error, NSString *des))failed
{
    
    NSString *url = [NSString stringWithFormat:@"%@/busi/payment/payment",
                     HOST_Progress];
    
    NSDictionary *parameters = @{
                                 @"userId":StringValue(userId),
                                 @"payWay":@"3",//微信支付
                                 @"amount":StringValue(amount),
                                 @"goodsType":[NSString stringWithFormat:@"%@",@(goosType)],
                                 @"relationId":StringValue(relationId),
                                 @"type":[NSString stringWithFormat:@"%@",@(paymentType)]
                                 };
    
    [HTTP POST:url
    parameters:parameters
       success:^(id responseObject, NSString *des)
    {
        SGGeneratedWeixinPayModel *model = [SGGeneratedWeixinPayModel c_modelWithDic:responseObject[@"bring"]];
        
        if (success) {
            success(model,des);
        }
        
    } failure:^(NSError *error, NSString *des) {
        if (failed) {
            failed(error,des);
        }
    }];
}





@end
